﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.DynamicProxy;
using DevFx.FrameWork.Attribute;
using DevFx.FrameWork.Exception;

namespace DevFx.FrameWork.Log
{
    public class LogHelper
    {
        //是否正在记录日志
        private bool _IsLogging = false;
        //默认是需要写日志
        private bool _IsLog = true;
        /// <summary>
        /// 是否正在记录日志
        /// </summary>
        public bool IsLogging
        {
            get
            {
                return _IsLogging;
            }
            set
            {
                _IsLogging = value;
            }
        }
        /// <summary>
        /// 是否需要记录日志
        /// </summary>
        public bool IsLog
        {
            get
            {
                return _IsLog;
            }
            set
            {
                _IsLog = value;
            }
        }

        public void WritePreLog(IInvocation invocation)
        {
            try
            {
                if (IsNeedLog(invocation))
                {
                    WritePre(invocation);
                }
            }
            catch (FrameException ex)
            {
                throw ex;
            }
            finally
            {
                //IsLogging = false;
            }
        }
        public void WriteAfterLog(IInvocation invocation)
        {
            try
            {
                if (IsNeedLog(invocation))
                {
                    WriteAfter(invocation);
                }
            }
            catch (FrameException ex)
            {
                throw ex;
            }
            finally
            {
                //IsLogging = false;
            }
        }
        /// <summary>
        /// 在事件之前写入Log
        /// </summary>
        /// <param name="invocation"></param>
        private void WritePre(IInvocation invocation)
        {
            //ILog log = LogHelper.CreateLogInstance();
            //log.BeginTime = DateTime.Now;
            //log.LogLevel = LogLevel.Application;
            //log.LogType = LogType.MethodLog;
            ////调用的类名
            //Type targetClass = invocation.InvocationTarget.GetType();
            //log.TargetClassName = targetClass.Name;
            //log.TargetClassFullName = targetClass.FullName;
            ////调用的方法名
            //log.TargetMethodName = invocation.Method.Name;
            //if (LogSetting.IsLogParam)
            //{
            //    //保存输入的参数
            //    ParameterInfo[] logParams = invocation.Method.GetParameters();
            //    for (int i = 0; i < logParams.Length; i++)
            //    {
            //        ParameterInfo param = logParams[i];
            //        ILogParam logParam = LogHelper.CreateLogParamInstance();
            //        logParam.ParamName = param.Name;
            //        logParam.ParamTypeName = param.ParameterType.Name;
            //        logParam.ParamTypeFullName = param.ParameterType.FullName;
            //        logParam.ParamDirection = LogParamDirection.Input;
            //        object o = args[i];
            //        //序列化参数的值
            //        logParam.ParamValue = Serializer.SerializeToXml(o);
            //        log.LogParams.Add(logParam);
            //    }
            //}
        }
        /// <summary>
        /// 在事件之后写入Log
        /// </summary>
        /// <param name="invocation"></param>
        private void WriteAfter(IInvocation invocation)
        {

        }

        private bool IsNeedLog(IInvocation invocation)
        {
            bool result = false;
            if (!IsLog)
            {
                result = false;
                return result;
            }
            object[] logAttrs = invocation.Method.GetCustomAttributes(typeof(LogAttribute), false);
            if (logAttrs != null && logAttrs.Length > 0)
            {
                WritePre(invocation);
                result = true;
            }
            return result;
        }
    }
}
